#include "symbol.h"
#include "errorHandler.h"

symbol newSymbol(enum type_symbol type,void *val,enum val_t valT)
{
  symbol news;
  news.type=type;
  if ((type==S_ID)||(type==S_TMP))
    {
      /* pointer casting */
      const char* valName=(char*)val;
      news.val.name=strdup(valName);
    }
  else if (type==S_CST)
    {
      if (valT==TYPE_STR)
	{
	  /* pointer casting */
	  const char* valStr=(char*)val;
	  news.val.strcst=strdup(valStr);
	}
      else
	{
	  const int* valInt = (int*) val;
	  news.val.cst=*(valInt);
	}
    }
  news.valType=valT;
  news.size=1;
  news.quadRef=0;
  return news;
}

void printSymbol(symbol s)
{
  printf("type ");
  printValType(s.valType);
  printf(" size:%d\t",s.size);
  switch (s.type)
    {
    case (S_ID):
      {
	if (s.valType==TYPE_UNIT)
	  printf("quadRef:%li ",s.quadRef);
	printf("type:S_ID: value:%s\n",s.val.name);
      }
      break;
    case (S_CST):
      {
	if (s.valType==TYPE_STR)
	  printf("type:S_CST: value:%s\n",s.val.strcst);
	else
	  printf("type:S_CST: value:%d\n",s.val.cst);
      }
      break;
    case (S_TMP):
      {
	printf("type:S_TMP: value:%s\n",s.val.name);
      }
      break;
    }
}

void printValType(enum val_t valtype)
{
  switch(valtype)
    {
    case(TYPE_BOOL):
      printf("BOOL");
      break;
    case(TYPE_INT):
      printf("INT");
      break;
    case(TYPE_BOOL_REF):
      printf("BOOL_REF");
      break;
    case(TYPE_INT_REF):
      printf("INT_REF");
      break;
    case(TYPE_BOOL_ARRAY):
      printf("BOOL_ARRAY");
      break;
    case(TYPE_INT_ARRAY):
      printf("INT_ARRAY");
      break;
    case(TYPE_UNIT):
      printf("UNIT");
      break;
    case(TYPE_STR):
      printf("STR");
      break;
    }
}

void setSizeVal(symbol* pt,unsigned int size) 
{
  pt->size=size;
}
